from iFinDPy import *
from DataProcess.Ifindfunctions import IFind_DailyQuote
import chardet
import datetime
import pandas as pd
import json

# ---更新公募基金全部细节---
def Download_Industry(database, profile_date, level=1):
    #
    df_instrument = database.Get_Instruments_DataFrame(instrument_type="Stock")

    symbols = list(df_instrument["symbol"])
    # symbols = symbols[:10]
    str_symbols = ",".join(symbols)

    # 确定行业分类类型 申万 中信
    industry_type = "GICS"
    if industry_type == "GICS":
        str_request_fields = "ths_the_gics_industry_stock"
    else:
        str_request_fields = ""
    level_param = 100 + level - 1
    str_params = str(level_param) + "," + profile_date.strftime('%Y-%m-%d')
    #
    b_data = THS_BasicData(str_symbols, str_request_fields, str_params, True)
    guess_encoding = chardet.detect(b_data)
    # str1 = str(b_data, guess_encoding["encoding"])
    str1 = str(b_data, "gbk")
    # str1 = str1.strip()
    # str1 = str1.replace('\\n', '').replace('\\r', '')
    # str1 = str1.replace('\\', '')

    # data = eval(str1)
    # print(str1)
    data = json.loads(str1)

    #
    # ---Save to DataBase---
    documents = []
    for symbol_data in data["tables"]:
        symbol = symbol_data["thscode"]
        content = symbol_data["table"]
        #
        document = {}
        document["symbol"] = symbol
        document["datetime"] = document["date"] = profile_date
        document["type"] = industry_type
        document["level"] = level
        document["industry_name"] = content[str_request_fields][0]
        document["Key2"] = symbol + "_" + industry_type + "_" + str(level) + "_" + profile_date.strftime('%Y-%m-%d')
        #
        print("Update Industry Info", symbol)
        documents.append(document)
    #
    database.Upsert_Many("financial_data", "stock_industry_2", {}, documents)



if __name__ == '__main__':
    #
    # ---Connect to DataBase, Find Series 连接数据库---
    from Core.Config import *
    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()
    #
    a = THS_iFinDLogin('jd1079', '898132')
    #
    profile_date = datetime.datetime(2021,1,1)
    Download_Industry(database, profile_date, level=4)

    # b_data = THS_BasicData('000001.SZ,002594.SZ','ths_the_gics_industry_stock','102,2021-01-27', True)
    # str1 = str(b_data, "gbk")
    # data = json.loads(str1)
    # print(data)


